package com.bkhc.order.repository;


import com.alibaba.fastjson.JSONObject;
import com.bkhc.common.entity.User;
import com.bkhc.common.util.response.ResultJson;
import com.bkhc.common.vo.CustomerVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Component
public class BusinessRepository {

    @Lazy
    @Autowired
    private RestTemplate restTemplate;

    public CustomerVo queryCustomer(String userId){
        String serviceName = "BUSINESS-APP";
        String url = "http://" + serviceName + "/business/service/customer/queryByUserid?userId="+userId;
        ResultJson  result = restTemplate.postForObject(url, null, ResultJson.class);
        Map map = (Map) result.getData();
        if(map == null){
            return null;
        }
        JSONObject json = new JSONObject(map);
        ObjectMapper mapper = new ObjectMapper();
        CustomerVo u = mapper.convertValue(json,CustomerVo.class);
        return u;
    }
}
